home *** CD-ROM | disk | FTP | other *** search
- /// DemoIndexing.cp
- // Copyright: © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
-
-
- #include "DemoIndex.h"
-
- // include some utilities
- #include "HighFreqTerms.h"
- #include <stdio.h>
- #include <string.h>
-
- #ifdef DEBUG_NEW
- #include <DebugNew.h>
- #endif
-
- #if __profile__
- #include <profiler.h>
- #endif
-
- const char* gtoken = "product";
-
- //// IAIndex construction
-
- void DemoUpdate(StringPtr folder, bool initialize = true);
- void DemoUpdate(StringPtr folder, bool initialize) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- // IADeleteOnUnwind delStorage(storage);
- if (initialize) {
- storage->Initialize();
- } else {
- storage->Open(true);
- }
- // make an index in this storage
- DemoIndex* index = new DemoIndex(storage, folder);
- // IADeleteOnUnwind delIndex(index);
- if (initialize) {
- index->Initialize();
- } else {
- index->Open();
- }
- time_t start = time(NULL);
-
- index->Update(); // update the index
-
- storage->Commit(); // commit the changes
-
- time_t duration = time(NULL) - start;
- struct tm* time = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
- printf(buffer);
-
- delete index;
- delete storage;
- }
-
-
- void DemoHighFreqTerms();
- void DemoHighFreqTerms() {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(false);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- const uint32 maxResultCount = 10;
-
- printf("finding %lu highest frequency terms...\n", maxResultCount);
- time_t start = time(NULL);
-
- uint32 resultCount = maxResultCount;
- FreqTerm* results = GetHighFreqTerms(&index, &resultCount);
-
- time_t duration = time(NULL) - start;
- struct tm* time = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
- printf(buffer);
-
- for (uint32 i = 0; i < resultCount; i++) {
- FreqTerm* ft = results + i;
- printf("%lu %s\n", ft->freq, ft->term->GetData());
- delete ft->term;
- }
- IAFreeArray(results);
- }
-
- //// IAIndex compaction
-
- void DemoCompact ();
- void DemoCompact () {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
-
- printf("compacting...\n");
- time_t start = time(NULL);
-
- index.Compact();
- storage->Compact();
-
- time_t duration = time(NULL) - start;
- struct tm* time = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
- printf(buffer);
- }
-
- //void DemoMerge(StringPtr folder, bool initialize = true);
- void DemoMerge(StringPtr folder, bool initialize = false);
- void DemoMerge(StringPtr folder, bool initialize) {
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- DemoIndex* index = new DemoIndex(storage, folder);
- IADeleteOnUnwind delIndex(index);
- if (initialize) {
- storage->Initialize();
- index->Initialize();
- } else {
- storage->Open(true);
- index->Open();
- }
-
- IAStorage* storage1 = DEMOSTORAGE("\p1.index");
- IADeleteOnUnwind delStorage1(storage1);
- storage1->Open(false);
- DemoIndex index1(storage1);
- index1.Open();
- /*
- IAStorage* storage2 = DEMOSTORAGE("\p2.index");
- IADeleteOnUnwind delStorage2(storage2);
- storage2->Open(false);
- DemoIndex index2(storage2);
- index2.Open();
- */
- /*
- IAStorage* storage3 = DEMOSTORAGE("\p3.index");
- IADeleteOnUnwind delStorage3(storage3);
- storage3->Open(false);
- DemoIndex index3(storage3);
- index3.Open();
- */
-
- /* IAStorage* storage4 = DEMOSTORAGE("\p4.index");
- IADeleteOnUnwind delStorage4(storage4);
- storage4->Open(false);
- DemoIndex index4(storage4);
- index4.Open();
- */
- IAIndex* indices[3];
- indices[0] = &index1;
- //indices[1] = &index2;
- //indices[2] = &index3;
- //indices[3] = &index4;
-
- time_t start = time(NULL);
-
- index->Merge(indices, 1);
-
- storage->Commit();
- storage->Compact();
-
- time_t duration = time(NULL) - start;
- struct tm* time = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
- printf(buffer);
- }
-
- void DemoTermInfo ();
- void DemoTermInfo () {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- storage->Open(true);
- // make an index in this storage
- DemoIndex index(storage);
- index.Open();
- IATerm* term = new IATerm((unsigned char*)gtoken, strlen(gtoken));
- TermInfo* terminfo = index.GetTermInfo(term);
- printf ("Term Info before delete %d\n", terminfo->GetDocumentCount());
- DocID maxID = index.GetMaxDocID();
-
- if (maxID > 1) {
- IADoc* doc = index.GetIDDoc(maxID / 2);
- IADeleteOnUnwind delDoc(doc);
- time_t start = time(NULL);
-
- index.DeleteDoc(doc);
-
- time_t duration = time(NULL) - start;
- struct tm* time = localtime(&duration);
- char buffer[80];
- strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
- printf(buffer);
-
- index.Flush();
- storage->Commit(); // commit the changes
- }
- TermInfo* terminfo2 = index.GetTermInfo(term);
- printf ("Term Info after delete %d\n", terminfo2->GetDocumentCount());
-
- index.Flush();
- storage->Commit(); // commit the changes
-
- delete terminfo;
- delete terminfo2;
-
- delete term;
- }
-
- /// the main procedure
- void* MyAllocator(size_t size);
- void* MyAllocator(size_t size)
- {
- // printf ("called the local allocator for %d bytes\n", size);
- void* l = (void*)malloc(size);
- return l;
- }
-
- void MyDeAllocator(void* obj);
- void MyDeAllocator(void* obj)
- {
- // printf ("called the local Deallocator\n");
- free(obj);
- }
-
- void DemoOneStep(StringPtr folder, bool initialize = true);
- void DemoOneStep(StringPtr folder, bool initialize) {
- // make a storage
- IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
- IADeleteOnUnwind delStorage(storage);
- if (initialize) {
- storage->Initialize();
- } else {
- storage->Open(true);
- }
- // make an index in this storage
- DemoIndex* index = new DemoIndex(storage, folder);
- IADeleteOnUnwind delIndex(index);
- if (initialize) {
- index->Initialize();
- } else {
- index->Open();
- }
-
- index->Update(); // update the index
- index->Flush();
- index->Compact();
- storage->Commit(); // commit the changes
- storage->Compact();
-
-
- const uint32 maxResultCount = 10;
-
- uint32 resultCount = maxResultCount;
- FreqTerm* results = GetHighFreqTerms(index, &resultCount);
-
- for (uint32 i = 0; i < resultCount; i++) {
- char buffer[256];
-
- FreqTerm* ft = results + i;
- sprintf(buffer, "%lu %s\r", ft->freq, ft->term->GetData());
- delete ft->term;
- }
- IAFreeArray(results);
- }
-
- void DemoAdd();
- void DemoAdd()
- {
- StringPtr name = "\ptest.index";
- short vRefNum = 0;
- long dirID = 0;
- StringPtr docName = "\pEagle";
- short docVRefNum = 0;
- long docDirID = 0;
- // create storage for the index
- IAStorage * aStorage = MakeHFSStorage(vRefNum, dirID, name);
- IADeleteOnUnwind delInxStorage(aStorage);
- aStorage->Initialize();
- // create the corpus
- HFSCorpus* anHFSCorpus = new HFSCorpus(HFSCorpusType);
- // create the HFS Doc
- HFSDoc *anHFSDoc = new HFSDoc(anHFSCorpus, docVRefNum, docDirID, docName);
- // create index for folder (creates analysis)
- InVecIndex anInVecIndex(aStorage, anHFSCorpus, new SimpleAnalysis());
- anInVecIndex.Initialize();
- anInVecIndex.AddDoc(anHFSDoc);
- // complete the update
- anInVecIndex.Flush();
- // commit the storage to disk
- aStorage->Commit ();
- printf ("Successful Completion\n");
-
- }
- void main() {
-
-
- #if __profile__
- #ifdef powerc
- ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
- #else
- ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
- #endif
- #endif
-
- // make this bigger to use more memory and index faster
- IADiskBlockSize = 4096;
-
- // IAAllocationFunc = &MyAllocator;
- // IADeAllocationFunc = &MyDeAllocator;
- IATry {
- // enter the name of the folder to index here
-
- StringPtr folder = "\pMacintoshHD:Finder:VTwin:Examples:Projects:Test Collection";
- // StringPtr folder = "\pOldIpanema:Tests:Test Collection";
- // StringPtr folder = "\pFarzin2";
-
-
- // uncomment this section to build a new index
- // for (int i = 0; i < 3; i++) {
- // if (i == 0) {
- // DemoOneStep(folder);
- // }
- // else {
- // DemoOneStep(folder, false);
- // }
- // }
- // DemoAdd();
- DemoUpdate(folder);
-
- // DemoTermInfo();
- // DemoCompact();
-
- // DemoHighFreqTerms();
-
- // uncomment this section to update an existing index
- // DemoUpdate(folder, false);
- // DemoCompact();
- // DemoHighFreqTerms();
-
-
- // DemoMerge(folder);
- }
- IACatch (const IAException& exception) {
- printf("Caught exception: %s\n", exception.What());
- }
-
- IAReportMemoryUsage();
-
-
- #if __profile__
- ProfilerDump("\pDemoIndex.prof");
- ProfilerTerm();
- #endif
-
- #ifdef DEBUG_NEW
- DebugNewReportLeaks();
- #endif
- }
-
-